// pages/details/details.js
import {ajax} from "../../utils/httpRequest.js";
import {URI_GETGOODDETAILS} from "../../api.js";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    show_attr_picker: false,
    img_hide: "",
    hide: 'hide',
    form: {
      number: 1,
    },
    goods: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //console.log(options.skuId);
    wx.setNavigationBarTitle({
      title: options.goodsName
    })
    this.getDetails(options.skuId);
  },
  getDetails(id) {
    ajax.get(URI_GETGOODDETAILS, {
      skuId: id
    }, {}, res => {
      console.log(res.data.data);
      this.setData({
        goods: res.data.data
      })
    });
  },
  showAttrPicker() {
    this.setData({
      show_attr_picker: true,
    });
  },
  hideAttrPicker() {
    this.setData({
      show_attr_picker: false
    });
  },

  addCart: function () {
    this.submit('ADD_CART');
  },

  buyNow() {
    this.submit('BUY_NOW');
  },

  submit: function (type) {
    var page = this;
    if (!page.data.show_attr_picker) {
      page.setData({
        show_attr_picker: true,
      });
      return true;
    }
    if (type == 'BUY_NOW') {//立即购买
      page.setData({
        show_attr_picker: false,
      });
      wx.redirectTo({
        url: `/pages/order-submit/order-submit?skuId=${this.data.goods.skuId}&num=${this.data.form.number}`
      });
      // wx.navigateTo({
      //   url: `/pages/order-submit/order-submit?skuId=${1}&num=${}`
      // })
    }

  },

  onGoodsImageClick: function (e) {

    // var urls = [];
    var index = e.currentTarget.dataset.index;
    //console.log(page.data.goods.pic_list);
    // for (var i in page.data.goods.pic_list) {
    //   urls.push(page.data.goods.pic_list[i].pic_url);
    // }
    wx.previewImage({
      urls: this.data.banner_list, // 需要预览的图片http链接列表
      current: this.data.banner_list[index],
    });
  },
  hide: function (e) {
    if (e.detail.current == 0) {
      this.setData({
        img_hide: ""
      });
    } else {
      this.setData({
        img_hide: "hide"
      });
    }
  },
  play: function (e) {
    var url = e.target.dataset.url;//获取视频链接
    this.setData({
      url: url,
      hide: '',
    });
    var videoContext = wx.createVideoContext('video');
    videoContext.play();
  },
  videoClose() {
    this.setData({
      hide: 'hide',
    });
    var videoContext = wx.createVideoContext('video');
    videoContext.pause();
  },
  numberSub: function () {
    var page = this;
    var num = page.data.form.number;
    if (num <= 1)
      return true;
    num--;
    page.setData({
      form: {
        number: num,
      }
    });
  },

  numberAdd: function () {
    var page = this;
    var num = page.data.form.number;
    num++;
    page.setData({
      form: {
        number: num,
      }
    });
  },

  numberBlur: function (e) {
    var page = this;
    var num = e.detail.value;
    num = parseInt(num);
    if (isNaN(num))
      num = 1;
    if (num <= 0)
      num = 1;
    page.setData({
      form: {
        number: num,
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function (res) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '择天惠-一个做优惠的小程序',
      path: `pages/details/details`,
      success: function (res) {
        console.log('转发成功');
      },
      fail: function (err) {
        console.log('转发失败')
      }
    }
  }
})